<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加</title>
</head>
<link rel="stylesheet" href="/static/layui/css/layui.css" />
<body>
<form class="layui-form" id="user-add"  action="" lay-filter="add">
    <input type="hidden" name="hid">
    <div class="layui-form-item">
        <label class="layui-form-label">姓名：</label>
        <div class="layui-input-inline">
            <input type="text" name="name" lay-verify="required" lay-reqtext=" 姓名不能为空！！" autocomplete="off" placeholder="请输入姓名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">身份证号：</label>
        <div class="layui-input-inline">
            <input type="text" name="id_card" lay-verify="identity" placeholder="请输入身份证号" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">民族：</label>
        <div class="layui-input-inline">
            <input type="text" name="nationality" lay-verify="required" lay-reqtext="民族不能为空！！" autocomplete="off" placeholder="民族：如--汉" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">出生日期：</label>
        <div class="layui-input-inline">
            <input type="text" name="birthday" id="birthday" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">住址：</label>
        <div class="layui-input-inline">
            <input type="text" name="address" lay-verify="required" lay-reqtext=" 住址不能为空！！" autocomplete="off" placeholder="住址" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">卡号：</label>
        <div class="layui-input-inline">
            <input type="text" name="card_id" lay-verify="required" lay-reqtext="卡号不能为空！！" autocomplete="off" placeholder="请输入卡号" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机：</label>
        <div class="layui-input-inline">
            <input type="tel" name="telphone" lay-verify="required|phone" autocomplete="off" placeholder="请输入手机" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别：</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男">
            <input type="radio" name="sex" value="女" title="女" checked>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" onclick="getDefaultGetCardId()">二代身份阅读</button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="getIDcardInfor()">读取就诊卡</button>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
</body>

<script src="/static/js/jquery-3.3.1.js"></script>
<script src="/static/layui/layui.js"></script>
<script>
    //二代身份阅读
    function getIDcardInfor() {
        $.ajax({
            url: "/patient/findById",
            type: "post",
            dataType: "json",
            success: function (data) {
                alert(data)
                if (null != data.idcard) {
                    $("#IDname").val(data.name);
                    $("#IDcard").val(data.idcard);
                    $("#IDsex").val(data.sex);
                    $("#IDnationality").val(data.nationality);
                    $("#IDbirthday").val(data.birth);
                    $("#IDaddress").val(data.address);
                } else {
                    swal(data.message, "", "error")
                }
            }
        })
    }
    //读取就诊卡
    function getDefaultGetCardId() {
        $.ajax({
            url: "/register/findById",
            type: "post",
            success: function (data) {
                if (data.data === "fail") {
                    swal("读卡失败！请刷新页面重试", "", "error")

                } else if (data.data === "none") {
                    swal("未识别到卡片！", "", "error")
                }else {
                    $("#cardId_1").val(data.data.card_id)
                }
            }
        })
    }
    //Demo
    layui.use(['form', 'laydate'], function () {
        var form = layui.form, laydate = layui.laydate;
        laydate.render({
            elem: '#birthday'
        });
        //监听提交
        form.on('submit(formDemo)', function (data) {
            var data1 = form.val("add");
            $.ajax({
                url: '/patient/add',
                type: "post",
                dataType: "json",
                data: data1,
                success: function (da) {
                    console.log(da)
                    if (da.code == 200) {
                        layer.alert("添加成功！", function () {
                            // window.parent.location.reload();//刷新父页面
                            // parent.layer.close(layer.index);//关闭弹出层
                            alert("就诊卡办理成功！");
                            parent.layer.closeAll();
                        });
                    } else if(da.msg === "ACTIVATED"){
                        layer.msg("该就诊卡已被激活！");
                    }
                    else if(da.msg === "FAIL"){
                        layer.msg("办理异常！请刷新页面重新操作！");
                    }
                    else if(da.msg === "COVER"){
                        layer.msg("该患者已有就诊卡，点击“确认”进行补办操作！");
                        layer.confirm('该患者已有就诊卡，点击“确认”进行补办操作！', function (index) {
                            $.ajax({
                                type: "POST",
                                url: "/patient/update",
                                data: data1,
                                dataType: "json",
                                success: function (da) {
                                    if (da.code == 200) {
                                        layer.msg("就诊卡补办成功！")
                                        alert("就诊卡补办成功!!")
                                        parent.layer.closeAll();
                                    } else {
                                        layer.msg("办理异常！请刷新页面重新操作")
                                    }
                                },
                                error: function (da) {
                                    alert("出现错误");
                                    return false;
                                }
                            }) //ajax结束
                            layer.close(index);
                        });
                    }
                }
            });
            return false;
        });
    });
</script>
</html>